<?php
namespace Home\Controller;
use Think\Controller;
use OSS\OssClient; // | OSS客户端类，用户通过OssClient的实例调用接口 |
use OSS\Core\OssException; // | OSS异常类，用户在使用的过程中，只需要注意这个异常|
require_once "./ThinkPHP/Library/Vendor/aliyunOss/autoload.php"; //引入这个阿里云文件
class UploadController extends Controller {
    public function index(){
    
        var_dump(M('photo')->select());
        die;
    }
	public function upload($file,$file_new,$img_name){
		 //oss上传 
		$bucketName = C('OSS_TEST_BUCKET'); 
		
		$ossClient = new OssClient(C('OSS_ACCESS_ID'), C('OSS_ACCESS_KEY'), C('OSS_ENDPOINT'), false); 
		$web=C('OSS_WEB_SITE'); 
		//图片  
		$fFiles= $file;
	
		$object = 'phone/'.$file_new.'/'.$img_name;
			$ossClient->uploadFile($bucketName, $object, $fFiles); 
		 try{
			$ossClient->uploadFile($bucketName, $object, $fFiles); 
			return $object;
		} catch(OssException $e) {
			
			return false;
		}
		
	
		
	}
	
	public function fileList(){
		
		$dir = "D:/image/";
		
		$dir_list=scandir($dir);  
	
		foreach($dir_list as $k=>$file){  
                        $data = array();
			if($file!='..' && $file!='.'){  
					$dir_img = array();
					$dir_img  = scandir($dir.'/'.$file);
					$img_list[$k]['name'] = $file;
					$str_img = '';
                                        $data['name'] = $file;
                                        $imgstr = '';
                                         $file = iconv("gb2312","UTF-8", $file);
					foreach($dir_img as $key=>$img_name){  
						
						if($img_name!='..' && $img_name!='.'){  
							$name = $img_name;
						   $result = $this->upload($dir.$file.'/'.$img_name,$file,$img_name);
						   $imgstr = $imgstr.C('OSS_WEB_SITE').'/'.$result.',';
                                                  
                                                 
						   $data['image'] = C('OSS_WEB_SITE').'/'. $result;
						}
				
				 
			}  
                         $data['imgList'] = $imgstr;
                         $data['addtime'] = time();
                       
                       
                         if(!empty($data['image'])){
                             M('photo')->add($data);
                             echo  M('photo')->getLastSql().'</br>';
                         }
                        
		}  
			
		}
	}
}